<template>
  <div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      openId: null
    };
  },
  created () {
    let ua = window.navigator.userAgent.toLowerCase();
    //微信浏览器打开
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
      let code = this.$route.query.code;
      if (code == null) {
        window.location.href =
          "http://xxxx/get-weixin-code.html?appid=" +
          this.$api.baseAppId +
          "&scope=snsapi_base&state=1" +
          "&redirect_uri=" +
          encodeURIComponent(this.$api.authPage);
      } else {
        this.login(code);
      }
    } else {
      alert("请在微信浏览器中打开");
      console.log("请在微信浏览器中打开");
    }
  },
  methods: {
    login (code) {
      this.$http
        .doPost(this.$api.loginApi, {
          code: code,
          appId: this.$api.baseAppId
        })
        .then(res => {
          if (res.state) {
            let rs = {
              openId: res.data.openId,
              uid: res.data.uid
            }
            this.$store.commit("initUser", rs);
            this.$router.push("/");
          } else {
            console.log("失败");
            window.location.href =
              "https://xxxx/get-weixin-code.html?appid=" +
              this.$api.baseAppId +
              "&scope=snsapi_base&state=1" +
              "&redirect_uri=" +
              encodeURIComponent(this.$api.authPage);
          }
        });
    },
  }
};
</script>
<style lang="less" scoped>
</style>
